/**
 * ResourceAction.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie.swing;

import java.awt.event.*;
import java.beans.*;
import java.util.*;

import javax.swing.*;

import org.windu2b.jcaddie.tools.*;

/**
 * @author windu.2b
 * 
 */
public class ResourceAction extends AbstractAction
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1678800702097231903L;


	public static final String	POPUP	         = "Popup";




	public ResourceAction( ResourceBundle resource, String actionPrefix )
	{
		this( resource, actionPrefix, false );
	}




	public ResourceAction( ResourceBundle resource, String actionPrefix,
	        boolean enabled )
	{
		String propertyPrefix = actionPrefix + ".";
		String name = resource.getString( propertyPrefix + NAME );

		putValue( NAME, name );
		putValue( DEFAULT, name );

		String shortDescription = getOptionalString( resource, propertyPrefix
		        + SHORT_DESCRIPTION );
		if ( shortDescription != null )
		    putValue( SHORT_DESCRIPTION, shortDescription );

		String longDescription = getOptionalString( resource, propertyPrefix
		        + LONG_DESCRIPTION );
		if ( longDescription != null )
		    putValue( LONG_DESCRIPTION, longDescription );

		String smallIcon = getOptionalString( resource, propertyPrefix
		        + SMALL_ICON );

		if ( smallIcon != null )
		{
			putValue( SMALL_ICON, new ImageIcon( ImageInJAR.getInstance().getImage( smallIcon ) ) );
			/*putValue( SMALL_ICON, new ImageIcon( getClass().getResource(
			        ".." + File.separator + URLIcon.RESOURCES_DIR.toString()
			                + File.separator + smallIcon ) ) );*/
		}

		String propertyKey = propertyPrefix + ACCELERATOR_KEY;
		String acceleratorKey = getOptionalString( resource, propertyKey + "."
		        + System.getProperty( "os.name" ) );
		if ( acceleratorKey == null )
		    acceleratorKey = getOptionalString( resource, propertyKey );

		if ( acceleratorKey != null )
		    putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( acceleratorKey ) );

		String mnemonicKey = getOptionalString( resource, propertyPrefix
		        + MNEMONIC_KEY );
		if ( mnemonicKey != null )
		    putValue( MNEMONIC_KEY, Integer.valueOf( mnemonicKey.charAt( 0 ) ) );

		// On définit si la ressource est accessible ou non
		setEnabled( enabled );
	}




	private String getOptionalString( ResourceBundle resource,
	        String propertyKey )
	{
		try
		{
			return resource.getString( propertyKey );
		}
		catch ( MissingResourceException ex )
		{
			return null;
		}
	}




	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed( ActionEvent arg0 )
	{
		throw new UnsupportedOperationException();
	}



	/**
	 * An action decorator for menu items.
	 */
	public static class MenuAction implements Action
	{
		private Action	action;




		public MenuAction( Action action )
		{
			this.action = action;
		}




		public void actionPerformed( ActionEvent ev )
		{
			this.action.actionPerformed( ev );
		}




		public void addPropertyChangeListener( PropertyChangeListener listener )
		{
			this.action.addPropertyChangeListener( listener );
		}




		public Object getValue( String key )
		{
			// Avoid mnemonics, tooltips and icons in Mac OS X menus
			if ( System.getProperty( "os.name" ).startsWith( "Mac OS X" )
			        && ( key.equals( MNEMONIC_KEY ) || key.equals( SMALL_ICON ) || key
			                .equals( SHORT_DESCRIPTION ) ) )
			{
				return null;
			}
			return this.action.getValue( key );
		}




		public boolean isEnabled()
		{
			return this.action.isEnabled();
		}




		public void putValue( String key, Object value )
		{
			this.action.putValue( key, value );
		}




		public void removePropertyChangeListener(
		        PropertyChangeListener listener )
		{
			this.action.removePropertyChangeListener( listener );
		}




		public void setEnabled( boolean enabled )
		{
			this.action.setEnabled( enabled );
		}
	}

	/**
	 * An action decorator for popup menu items.
	 */
	public static class PopupAction extends MenuAction
	{
		public PopupAction( Action action )
		{
			super( action );
		}




		public Object getValue( String key )
		{
			// If it exists, return POPUP key value if NAME key is required
			if ( key.equals( NAME ) )
			{
				Object value = super.getValue( POPUP );
				if ( value != null )
				{
					return value;
				}
			}
			else if ( key.equals( SMALL_ICON ) )
			{
				// Avoid icons in popus
				return null;
			}
			else if ( System.getProperty( "os.name" ).startsWith( "Mac OS X" )
			        && key.equals( ACCELERATOR_KEY ) )
			{
				// Avoid accelerators in Mac OS X popups
				return null;
			}
			return super.getValue( key );
		}
	}
}